import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "home",
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
    
  },
  {
    path: "/trip",
    name: "trip",
    component: () => import("../views/TripView.vue"),
  },
  {
    path: "/payment",
    name: "payment",
    component: () => import("../views/PaymentView.vue"),
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/MyView.vue"),
    // beforeEnter: (to, from, next) => {
    //   let token = window.localStorage.getItem("token");
    //   if (!token) {
    //     next("/login");
    //     return;
    //   } else {
    //     next();
    //   }
    // },
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/LoginView.vue"),
  },
  {
    path: "/search",
    name: "search",
    component: () => import("../views/SearchView.vue"),
  },
  {
    path: "/group",
    name: "group",
    component: () => import("../views/GrouptourView.vue"),
  },
  {
    path: "/page",
    name: "page",
    component: () => import("../views/PageView.vue"),
  },
  {
    path: "/pay",
    name: "pay",
    component: () => import("../views/PayView.vue"),
    //独享路由守卫
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("token");
      //判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path: "/collection",
    name: "collection",
    component: () => import("../views/CollectionView.vue"),
  },
  {
    path: "/footprint",
    name: "footprint",
    component: () => import("../views/FootprintView.vue"),
  },
];

// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(){
    return {x:0,y:0}
  }
});

// const router = createRouter({
//   scrollBehavior(to, from, savedPosition) {
//     // 始终滚动到顶部
//     return { top: 0 }
//   },
// })

export default router;
